layui.use(['table', 'jquery', 'layer'], function(){
        let table = layui.table;
        let $ = layui.jquery;
        let layer = layui.layer;
        let form = layui.form;
        // 初始化数据表格
        table.render({
            elem: '#dataTable',
            id: 'dataTable',
            url: './ajax/qrlistgetDataList',
            page: true,
            cols: [
                [
                    {
                        field: 'id', 
                        title: '#', 
                        width: 60
                        
                    },
                    {
                        field: 'data', 
                        title: '数据',
                        width: 180,
                        templet:function(d){
    					    return '<img src="/assets/icon/'+d.type +'.ico" width="16" >'+ d.typename + `<br>备注：[${d.beizhu}]`;
    					}
                        
                    },
                    {
                        field: 'money', 
                        title: '金额', 
                        width: 60
                    },
                    {
                        field: 'time', 
                        title: '更新时间/运行时间', 
                        templet:function(d){
                            return d.cookie_zt  + `<br>${d.addtime}<br>${d.currentTime}`
                        }
                    },
                    {
                        title: '操作', 
                        toolbar: '#tableBar', 
                        width: 300, 
                        align:'center'
                    }
                ]
            ],
            cellMinWidth: 100,
			toolbar: '#toolbar',
			defaultToolbar: [{
				title: '刷新',
				layEvent: 'refresh',
				icon: 'layui-icon-refresh',
			}, 'filter', 'print', 'exports'],
			done: function(res, curr, count){
			    loadBaseInfo();
			}
        });
        
        // 工具栏事件处理
        table.on('toolbar(dataTable)', function(obj){
            if (obj.event === 'add') {
				window.add();
			} else if (obj.event === 'refresh') {
				window.refresh();
			}else if(obj.event === 'uid'){
			    window.showUIDQrCodePopup();
			}
        });
        
        // 行工具事件处理
        table.on('tool(dataTable)', function(obj){
            if (obj.event === 'update') {
				window.Get_Qr(obj);
			} else if(obj.event === 'edit'){
			    window.edit(obj);
			} else if(obj.event === 'del'){
			    window.remove(obj);
			}
        });
        window.loadBaseInfo = function(){
            $.ajax({
    			url: "./ajax/info_qrlist",
    			dataType: 'json',
    			type: 'post',
    			success: function(res) {
    				if (res.code == 200) {
    					['alipay', 'wxpay', 'qqpay'].forEach(type => {
                            ['success', 'fail'].forEach(field => {
                                $(`#${type}${field}`).text(res[type][field]);
                            });
                        });
                        
                        $("#allqrlist").text(res.allqrlist);
                        $("#maxqrlist").text(res.maxqrlist);
    				} else {
    					layer.msg(res.msg, {
    						icon: 2,
    						time: 1000
    					});
    				}
    			},
    			error:function(){
    			    layer.msg("服务器错误！", {
                        icon: 2,
                        time: 1000
                    });
    			}
    		})
        }
        window.screen = function () {
        	if (typeof width !== 'number' || width === 0) {
        		width = $(window).width() * 0.8;
        	}
        	if (typeof height !== 'number' || height === 0) {
        		height = $(window).height() * 0.5;
        	}
        	return [width + 'px', height + 'px'];
        }
        window.remove = function(obj){
            layer.confirm('确定要删除该二维码吗？', {
					icon: 3,
					title: '提示'
				}, function(index) {
					layer.close(index);
					let loading = layer.load();
					$.ajax({
						url: './ajax/del_qrlist',
						data: {
							id: obj.data['id']
						},
						dataType: 'json',
						type: 'POST',
						success: function(res) {
							layer.close(loading);
							//判断有没有权限
							if (res.code == 200) {
								layer.msg(res.msg, {
									icon: 1,
									time: 1000
								}, function() {
									table.reload('dataTable');
								});
							} else {
								layer.msg(res.msg, {
									icon: 2,
									time: 1000
								});
							}
						},
    					error:function(){
    					    layer.close(loading);
    					    layer.msg("服务器错误！", {
                                icon: 2,
                                time: 1000
                            });
    					}
					})
				});
        }
        // 打开新增窗口
        window.add = function() {
            layer.open({
                type: 2,
                title: '新增二维码',
                shade: 0.1,
                maxmin: true,
                area: screen(),
                content: 'qrlist/add'
            });
        }
        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改/查看通道数据',
                shade: 0.1,
                maxmin: true,
                area: screen(),
                content: 'qrlist/edit?id=' +  obj.data.id
            });
        }
        form.on('submit(editSave)', function(data){
            $.ajax({
    			data: JSON.stringify(data.field),
    			url: "/user/ajax/edit_qrlist",
    			contentType: 'application/json',
    			dataType: 'json',
    			type: 'post',
    			success: function(res) {
    				if (res.code == 200) {
    					layer.msg(res.msg, {
    						icon: 1,
    						time: 1000
    					}, function() {
    						parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
    						parent.layui.table.reload("dataTable");
    					});
    				} else {
    					layer.msg(res.msg, {
    						icon: 2,
    						time: 1000
    					});
    				}
    			},
    			error:function(){
    			    layer.msg("服务器错误！", {
                        icon: 2,
                        time: 1000
                    });
    			}
    		})
            return false;
        });
        window.refresh = function(param) {
			table.reload('dataTable');
		}
		var template = function(id) {
			return $(id).html();
		}
		window.showUIDQrCodePopup = function(){
    		    layer.open({
                    type: 1,
                    title: '支付宝 uid获取',
                    area: screen(),
                    shadeClose: true,
                    content: template('#uid'),  // 使用模板函数获取HTML
                    cancel: function(){
                        window.location.reload();
                    }
              });
		}
		window.showQrCodePopup = function(res){
    		    layer.open({
                    type: 1,
                    title: '更新二维码',
                    area: screen(),
                    shadeClose: true,
                    content: template('#qrcode'),  // 使用模板函数获取HTML
                    success: function(layero, index){  // 新增 success 回调函数
                        Popupcontent(layero , res);
                    },
                    cancel: function(){
                        window.location.reload();
                    }
              });
		}
});
